/*
 *  DatabaseSingleton.java 
 *    
 *    Copyright 2012 YAWP Project Team
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.googlecode.yawp.model;

import java.sql.*;

/**
 * The DatabaseSingleton class uses a singleton pattern 
 *   in order to ensure there is only one database connection.
 *   
 * @author Grace Hopkins
 *
 */
public class DatabaseSingleton {
	public static DatabaseSingleton ref;
	private Connection connection;
	
	/**
	 * PRIVATE constructor to keep control of when it is invoked.
	 *   
	 */
    private DatabaseSingleton() {
	    try {
	    	// Load the org.sqlite.JDBC.Driver class from the sqlite jar
		    Class.forName("org.sqlite.JDBC");
	    }catch (ClassNotFoundException e) {
	    	e.printStackTrace();
	    }
	}
    
    /**  
     * This function creates the Singleton Object at
     *   runtime. Any calls to this function after the
     *   first one will return the same object that was
     *   created in the first call.
     *   
     */
    
    public static DatabaseSingleton getDatabaseSingleton() {
        if(ref == null){
            ref = new DatabaseSingleton();
        }
        return ref;
    }
    
    /**  
     * Override the clone function in order to prevent copies.
     *   
     */
    public Object clone() throws CloneNotSupportedException {
        throw new CloneNotSupportedException();
    }
    
    /**  
     * Create the connection using the DriverManager class
     *   imported by the constructor.
     *   
     */
    public Connection getConnection(boolean autoCommit) {
        try {
        	connection = DriverManager.getConnection("jdbc:sqlite:yawp.db");
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return connection;
    }
}
